package com.lzc.tlmail.tlmailmenber.controller;

import java.util.Arrays;
import java.util.Map;


import com.lzc.common.exception.BizCodeEnume;
import com.lzc.common.vo.OauthUser;
import com.lzc.tlmail.tlmailmenber.exception.PhoneExitsException;
import com.lzc.tlmail.tlmailmenber.exception.UserNameExitsException;
import com.lzc.tlmail.tlmailmenber.feign.CouponsFeignservice;
import com.lzc.tlmail.tlmailmenber.vo.LoginVo;
import com.lzc.tlmail.tlmailmenber.vo.RegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lzc.tlmail.tlmailmenber.entity.MemberEntity;
import com.lzc.tlmail.tlmailmenber.service.MemberService;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.R;



/**
 * 会员
 *
 * @author lzc
 * @email 1029669636@gmail.com
 * @date 2020-07-26 19:25:14
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponsFeignservice couponsFeignservice;


    /**
     * 会员注册方法
     * @param registVo
     * @return
     */
    @RequestMapping("/regist")
    public R regist(@RequestBody RegistVo registVo){
        try {
            memberService.regist(registVo);
        }catch (UserNameExitsException e){
            return R.error(BizCodeEnume.USERNAME_EXCEPTION.getCode(),BizCodeEnume.USERNAME_EXCEPTION.getMsg());
        }catch (PhoneExitsException e){
            return R.error(BizCodeEnume.PHONE_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 社交登录
     * @param oauthUser
     * @return
     */
    @RequestMapping("/oauthLogin")
    public R oauthLogin(@RequestBody OauthUser oauthUser) throws Exception {
        MemberEntity entity=memberService.login(oauthUser);
        if(entity!=null){
            return R.ok().put("data",entity);
        }else {
            return R.error(BizCodeEnume.LOGIN_EXCEPTION.getCode(),BizCodeEnume.LOGIN_EXCEPTION.getMsg());
        }

    }

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        MemberEntity entity=memberService.login(loginVo);
        if(entity!=null){
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnume.LOGIN_EXCEPTION.getCode(),BizCodeEnume.LOGIN_EXCEPTION.getMsg());
        }

    }


    @RequestMapping("/member/list")
    public R test(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setNickname("张飞");
        R r = couponsFeignservice.memberCoupons();
        return R.ok().put("menber",memberEntity).put("coupons",r.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("tlmailmenber:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("tlmailmenber:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("tlmailmenber:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("tlmailmenber:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("tlmailmenber:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
